package org.abettor.pushbox.download;

import java.io.ByteArrayInputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.abettor.pushbox.model.MessageBoardBean;
import org.abettor.pushbox.model.NetAnswerBean;
import org.abettor.pushbox.model.NetPushBoxMapBean;
import org.abettor.pushbox.model.ResovedMapUserBean;
import org.abettor.pushbox.model.UserInfoBean;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import android.util.Log;

/**
 * 解析下载下来的数据
 * 
 * @author jzh
 * 
 */
public class ParaDownloadRetValue {
	public static final int HASMORE = 1, NOTHASMORE = 0, ERROR = -1;

	/**
	 * 解析答案的列表
	 * 
	 * @param content
	 * @return
	 */
	public static List<NetAnswerBean> paraAnswer(String content) {
		ByteArrayInputStream in = new ByteArrayInputStream(content.getBytes());
		List<NetAnswerBean> retList = new ArrayList<NetAnswerBean>();
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document doc = db.parse(in);
			NodeList list = doc.getElementsByTagName("v");
			for (int i = 0; i < list.getLength(); i++) {
				NetAnswerBean bean = new NetAnswerBean();
				Node node = list.item(i);
				NodeList nodes = node.getChildNodes();
				// 解析id
				int count = nodes.getLength();
				for (int j = 0; j < count; j++) {
					Node pairNode = nodes.item(j);
					String nodeName = pairNode.getNodeName();
					String tmp = pairNode.getFirstChild().getNodeValue();
					if ("id".equals(nodeName)) {
						bean.setId(Integer.parseInt(tmp));
					} else if ("upor".equals(nodeName)) {
						bean.setUpor(tmp);
					} else if ("upd".equals(nodeName)) {
						Date date = sdf.parse(tmp);
						bean.setUploadDate(date);
					} else if ("step".equals(nodeName)) {
						bean.setCount(Integer.parseInt(tmp));
					}else if("upid".equals(nodeName)){//设置用户的id
						bean.setUserId(Integer.parseInt(tmp));
					}
				}
				retList.add(bean);
			}
			in.close();
			return retList;
		} catch (Exception e) {
			Log.e(ParaDownloadRetValue.class.getName(), "", e);
		}
		return null;
	}
	public static List<ResovedMapUserBean> paraResovedUser(String content) {
		Log.i("content", content);
		ByteArrayInputStream in = new ByteArrayInputStream(content.getBytes());
		List<ResovedMapUserBean> retList = new ArrayList<ResovedMapUserBean>();
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document doc = db.parse(in);
			NodeList list = doc.getElementsByTagName("v");
			for (int i = 0; i < list.getLength(); i++) {
				ResovedMapUserBean bean = new ResovedMapUserBean();
				Node node = list.item(i);
				NodeList nodes = node.getChildNodes();
				// 解析id
				int count = nodes.getLength();
				for (int j = 0; j < count; j++) {
					Node pairNode = nodes.item(j);
					String nodeName = pairNode.getNodeName();
					String tmp = pairNode.getFirstChild().getNodeValue();
					if ("id".equals(nodeName)) {
						bean.setId(Integer.parseInt(tmp));
					} else if ("t".equals(nodeName)) {
						Date date = sdf.parse(tmp);
						bean.setUploadDate(date);
					}else if("ni".equals(nodeName)){
						bean.setResovler(tmp);
					}
				}
				retList.add(bean);
			}
			in.close();
			return retList;
		} catch (Exception e) {
			Log.e(ParaDownloadRetValue.class.getName(), "", e);
		}
		return null;
	}
	/**
	 * 解析回复的列表
	 * @param content
	 * @return
	 */
	public static List<MessageBoardBean> paraMessageBoardBean(String content){
		ByteArrayInputStream in = new ByteArrayInputStream(content.getBytes());
		List<MessageBoardBean> retList = new ArrayList<MessageBoardBean>();
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document doc = db.parse(in);
			NodeList list = doc.getElementsByTagName("v");
			for (int i = 0; i < list.getLength(); i++) {
				MessageBoardBean bean = new MessageBoardBean();
				Node node = list.item(i);
				NodeList nodes = node.getChildNodes();
				// 解析id
				int count = nodes.getLength();
				for (int j = 0; j < count; j++) {
					Node pairNode = nodes.item(j);
					String nodeName = pairNode.getNodeName();
					String tmp = pairNode.getFirstChild().getNodeValue();
					if ("id".equals(nodeName)) {
						bean.setId(Integer.parseInt(tmp));
					} else if ("ni".equals(nodeName)) {
						bean.setNickName(tmp);
					} else if ("t".equals(nodeName)) {
						Date date = sdf.parse(tmp);
						bean.setTime(date);
					} else if ("c".equals(nodeName)) {
						bean.setContent(tmp);
					}
				}
				retList.add(bean);
			}
			in.close();
			return retList;
		} catch (Exception e) {
			Log.e(ParaDownloadRetValue.class.getName(), "", e);
		}
		return null;
	}
	/**
	 * 解析用户信息
	 * @param content
	 * @return
	 */
	public static UserInfoBean paraUserInfo(String content){
		UserInfoBean bean = new UserInfoBean();
		ByteArrayInputStream in = new ByteArrayInputStream(content.getBytes());
		try {
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document doc = db.parse(in);
			NodeList list = doc.getElementsByTagName("r");
			Node node = list.item(0);
			NodeList nodes = node.getChildNodes();
			// 解析id
			int count = nodes.getLength();
			for (int j = 0; j < count; j++) {
				Node pairNode = nodes.item(j);
				String nodeName = pairNode.getNodeName();
				String tmp = pairNode.getFirstChild().getNodeValue();
				if(tmp != null && ! tmp.equals(""))
				{
					if ("rt".equals(nodeName)) {
						bean.setRegtime(tmp);
					} else if ("mu".equals(nodeName)) {
						bean.setMyUploadCount(tmp);
					} else if ("mr".equals(nodeName)) {
						bean.setMyResolvedCount(tmp);
					} else if ("mnu".equals(nodeName)) {
						bean.setMyQuesstionCount(tmp);
					} else if ("mnr".equals(nodeName)) {
						bean.setMyAnswerCount(tmp);
					}
				}
			}
			in.close();
			return bean;
		} catch (Exception e) {
			Log.e(ParaDownloadRetValue.class.getName(), "", e);
			return null;
		}
	}
	/**
	 * 解析用户的信息
	 * 
	 * @param content
	 * @param bean
	 */
	public static void paraUserInfo(String content, UserInfoBean bean) {
		ByteArrayInputStream in = new ByteArrayInputStream(content.getBytes());
		try {
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document doc = db.parse(in);
			NodeList list = doc.getElementsByTagName("r");
			Node node = list.item(0);
			NodeList nodes = node.getChildNodes();
			// 解析id
			int count = nodes.getLength();
			for (int j = 0; j < count; j++) {
				Node pairNode = nodes.item(j);
				String nodeName = pairNode.getNodeName();
				String tmp = pairNode.getFirstChild().getNodeValue();
				if(tmp != null && ! tmp.equals(""))
				{
					if ("rt".equals(nodeName)) {
						bean.setRegtime(tmp);
					} else if ("mu".equals(nodeName)) {
						bean.setMyUploadCount(tmp);
					} else if ("mr".equals(nodeName)) {
						bean.setMyResolvedCount(tmp);
					} else if ("mnu".equals(nodeName)) {
						bean.setMyQuesstionCount(tmp);
					} else if ("mnr".equals(nodeName)) {
						bean.setMyAnswerCount(tmp);
					}
				}
			}
			in.close();
		} catch (Exception e) {
			Log.e(ParaDownloadRetValue.class.getName(), "", e);
		}
	}

	/**
	 * 解析网络地图的列表
	 * 
	 * @param str
	 * @return
	 */
	public static List<NetPushBoxMapBean> paraNetMap(String content) {
		ByteArrayInputStream in = new ByteArrayInputStream(content.getBytes());
		List<NetPushBoxMapBean> retList = new ArrayList<NetPushBoxMapBean>();
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document doc = db.parse(in);
			NodeList list = doc.getElementsByTagName("v");
			for (int i = 0; i < list.getLength(); i++) {
				NetPushBoxMapBean bean = new NetPushBoxMapBean();
				Node node = list.item(i);
				NodeList nodes = node.getChildNodes();
				// 解析id
				int count = nodes.getLength();
				for (int j = 0; j < count; j++) {
					Node pairNode = nodes.item(j);
					String nodeName = pairNode.getNodeName();
					String tmp = pairNode.getFirstChild().getNodeValue();
					if ("id".equals(nodeName)) {
						bean.setId(Integer.parseInt(tmp));
					} else if ("upor".equals(nodeName)) {
						bean.setUploader(tmp);
					} else if ("upd".equals(nodeName)) {
						Date date = sdf.parse(tmp);
						bean.setUploadDate(date);
					} else if ("rc".equals(nodeName)) {
						bean.setResolvedCount(tmp);
					} else if ("mr".equals(nodeName)) {
						if ("1".equals(tmp)) {
							bean.setMyresolved(true);
						} else {
							bean.setMyresolved(false);
						}
					}
				}
				retList.add(bean);
			}
			in.close();
			return retList;
		} catch (Exception e) {
			Log.e(ParaDownloadRetValue.class.getName(), "", e);
		}
		return null;
	}

	/**
	 * 解析地图的详情
	 * 
	 * @param content
	 * @return
	 */
	public static NetPushBoxMapBean paraNetMapDetail(String content) {
		ByteArrayInputStream in = new ByteArrayInputStream(content.getBytes());
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document doc = db.parse(in);
			NodeList list = doc.getElementsByTagName("v");
			NetPushBoxMapBean bean = new NetPushBoxMapBean();
			Node node = list.item(0);
			NodeList nodes = node.getChildNodes();
			// 解析id
			int count = nodes.getLength();
			for (int j = 0; j < count; j++) {
				Node pairNode = nodes.item(j);
				String nodeName = pairNode.getNodeName();
				Node thenode = pairNode.getFirstChild();
				if(thenode==null){
					continue;
				}
				String tmp = thenode.getNodeValue();
				if ("id".equals(nodeName)) {
					bean.setId(Integer.parseInt(tmp));
				} else if ("upor".equals(nodeName)) {
					bean.setUploader(tmp);
				} else if("uporid".equals(nodeName)){
					bean.setUploaderId(Integer.parseInt(tmp));
				}else if ("upd".equals(nodeName)) {
					Date date = sdf.parse(tmp);
					bean.setUploadDate(date);
				} else if ("rc".equals(nodeName)) {
					bean.setResolvedCount(tmp);
				} else if ("mr".equals(nodeName)) {
					if ("1".equals(tmp)) {
						bean.setMyresolved(true);
					} else {
						bean.setMyresolved(false);
					}
				}else if("ps".equals(nodeName)){
					bean.setPs(tmp);
				}else if("fl".equals(nodeName)){
					bean.setFlowerCount(tmp);
				}else if("egg".equals(nodeName)){
					bean.setEggCount(tmp);
				}else if("rpl".equals(nodeName)){
					bean.setRplyCount(tmp);
				}
			}
			in.close();
			return bean;
		} catch (Exception e) {
			Log.e(ParaDownloadRetValue.class.getName(), "", e);
		}
		return null;
	}

	/**
	 * 检查是否有更多的地图
	 * 
	 * @param str
	 * @return 0没有更多的信息1更多
	 */
	public static int isHasMoreNetMap(String content) {
		ByteArrayInputStream in = new ByteArrayInputStream(content.getBytes());
		try {
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document doc = db.parse(in);
			NodeList list = doc.getElementsByTagName("h");
			Node node = list.item(0);
			String value = node.getFirstChild().getNodeValue();
			return Integer.parseInt(value);
		} catch (Exception e) {
			Log.e("x", "y", e);
			return ERROR;
		}
	}
	/**
	 * 检查是否有更多的用户
	 * @param content
	 * @return
	 */
	public static int isHasMoreResovedUser(String content) {
		ByteArrayInputStream in = new ByteArrayInputStream(content.getBytes());
		try {
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document doc = db.parse(in);
			NodeList list = doc.getElementsByTagName("h");
			Node node = list.item(0);
			String value = node.getFirstChild().getNodeValue();
			return Integer.parseInt(value);
		} catch (Exception e) {
			Log.e("x", "y", e);
			return ERROR;
		}
	}
}
